package com.xiangmo.travel.Controller;

import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.CollectDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Dvo.CollectDvo;
import com.xiangmo.travel.Dvo.TableCollectDvo;
import com.xiangmo.travel.Entity.Collect;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Result.PageToListResult;
import com.xiangmo.travel.Service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    //根据景点的id查询
    @GetMapping("/count/attraction/{id}")
    public ResultData<Long> getCollectNumByAttractionId(@PathVariable Long id){
        Long count = collectService.getCollectNumByAttractionId(id);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), count, null);
    }

    //根据用户的id查询
    @TokenRequired
    @GetMapping("/user/{id}")
    public ResultData<PageToListResult<CollectDvo>> getCollectListByUserId(@PathVariable Long id, @RequestParam int pageNum,
                                                                           @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageToListResult<CollectDvo> page = collectService.getCollectListByUserId(pageNum, pageSize, id);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
    }
    //如果同一个用户点同一个收藏，会报错，加校验
    //新增收藏
//    ======================================================
    @TokenRequired
    @PostMapping("/add")
    public ResultData<Collect> setCollect(@RequestBody CollectDto collectDto, @RequestHeader("Authorization") String token){
        if(collectService.isCollectByUser(collectDto) == 1){
            return new ResultData<>(CodeAndMsg.COLLECT_ERROR.getCode(),
                    CodeAndMsg.COLLECT_ERROR.getMessage(), null, token.substring(7));
        }
        int status = collectService.setCollect(collectDto);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
    }
    //删除收藏
    @TokenRequired
    @DeleteMapping("/cancel/{userId}/{attractionsId}")
    public ResultData<Collect> deleteCollectById(@PathVariable Long userId, @PathVariable Long attractionsId, @RequestHeader("Authorization") String token){
        int status = collectService.deleteCollectById(attractionsId, userId);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("删除收藏失败!");
        return new ResultData<>(CodeAndMsg.DELETE_ERROR.getCode(),
                CodeAndMsg.DELETE_ERROR.getMessage(), null, token.substring(7) );
    }
    //查询最热门的10篇景点文章

    //获取当前景点前6天收藏量
    @TokenRequired
    @GetMapping("/table/{id}")
    public ResultData<List<TableCollectDvo>> getTop6CollectByAttractionId(@PathVariable Long id){
        List<TableCollectDvo> list = collectService.getTop6CollectById(id);

        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), list, null);
    }
}
